//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Host;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Users;
using DotNetNuke.Framework;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Skins;
using DotNetNuke.Services.Personalization;
namespace DotNetNuke.Modules.Admin.Portals
{
	partial class SiteSettings : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		int intPortalId = -1;
		private string _ViewType = "";

		private string SelectedCultureCode
		{
			get { return LocaleController.Instance.GetCurrentLocale(PortalId).Code; }
		}

		private void BindDesktopModules()
		{
			DesktopModuleInfo desktopModule = null;
			Dictionary<int, DesktopModuleInfo> dicModules = DesktopModuleController.GetDesktopModules(Null.NullInteger);
			Dictionary<int, PortalDesktopModuleInfo> dicPortalDesktopModules = DesktopModuleController.GetPortalDesktopModulesByPortalID(intPortalId);
			foreach (PortalDesktopModuleInfo objPortalDesktopModule in dicPortalDesktopModules.Values)
			{
				dicModules.Remove(objPortalDesktopModule.DesktopModuleID);
			}
			ctlDesktopModules.AvailableDataSource = dicModules.Values;
			ctlDesktopModules.SelectedDataSource = dicPortalDesktopModules.Values;
		}

		private void LoadPortal(string activeLanguage)
		{
			DotNetNuke.Entities.Portals.PortalController objPortalController = new PortalController();
			DotNetNuke.Entities.Modules.ModuleController objModules = new ModuleController();
			UserController objUsers = new UserController();
			Common.Lists.ListController ctlList = new Common.Lists.ListController();
			Common.Lists.ListEntryInfoCollection colProcessor = ctlList.GetListEntryInfoCollection("Processor");
			cboProcessor.DataSource = colProcessor;
			cboProcessor.DataBind();
			cboProcessor.Items.Insert(0, new ListItem("<" + Services.Localization.Localization.GetString("None_Specified") + ">", ""));
			PortalInfo objPortal = objPortalController.GetPortal(intPortalId, activeLanguage);
			txtPortalName.Text = objPortal.PortalName;
			ctlLogo.FilePath = objPortal.LogoFile;
			ctlLogo.FileFilter = Common.Globals.glbImageFileTypes;
			txtDescription.Text = objPortal.Description;
			txtKeyWords.Text = objPortal.KeyWords;
			lblGUID.Text = objPortal.GUID.ToString().ToUpper();
			ctlBackground.FilePath = objPortal.BackgroundFile;
			ctlBackground.FileFilter = Common.Globals.glbImageFileTypes;
			txtFooterText.Text = objPortal.FooterText;
			optUserRegistration.SelectedIndex = objPortal.UserRegistration;
			ctlAudit.Entity = objPortal;

			PortalAliasController objPortalAliasController = new PortalAliasController();
			ArrayList arrPortalAliases;
			arrPortalAliases = objPortalAliasController.GetPortalAliasArrayByPortalID(intPortalId);
			if (DotNetNuke.Entities.Portals.PortalController.IsChildPortal(objPortal, Common.Globals.GetAbsoluteServerPath(Request)))
			{
				txtSiteMap.Text = Common.Globals.AddHTTP(Common.Globals.GetDomainName(Request)) + "/SiteMap.aspx?portalid=" + intPortalId.ToString();
			}
			else
			{
				if (arrPortalAliases.Count > 0)
				{
					PortalAliasInfo objPortalAliasInfo = (PortalAliasInfo)arrPortalAliases[0];
					txtSiteMap.Text = Common.Globals.AddHTTP(objPortalAliasInfo.HTTPAlias) + "/SiteMap.aspx";
				}
				else
				{
					txtSiteMap.Text = Common.Globals.AddHTTP(Common.Globals.GetDomainName(Request)) + "/SiteMap.aspx";
				}
			}
			optBanners.SelectedIndex = objPortal.BannerAdvertising;
			if (UserInfo.IsSuperUser)
			{
				lblBanners.Visible = false;
			}
			else
			{
				optBanners.Enabled = objPortal.BannerAdvertising != 2;
				lblBanners.Visible = objPortal.BannerAdvertising == 2;
			}
			List<TabInfo> listTabs = TabController.GetPortalTabs(TabController.GetTabsBySortOrder(intPortalId, activeLanguage, true), Null.NullInteger, true, "<" + Localization.GetString("None_Specified") + ">", true, false, false, false, false);
			cboSplashTabId.DataSource = listTabs;
			cboSplashTabId.DataBind();
			if (cboSplashTabId.Items.FindByValue(objPortal.SplashTabId.ToString()) != null)
			{
				cboSplashTabId.Items.FindByValue(objPortal.SplashTabId.ToString()).Selected = true;
			}
			cboHomeTabId.DataSource = listTabs;
			cboHomeTabId.DataBind();
			if (cboHomeTabId.Items.FindByValue(objPortal.HomeTabId.ToString()) != null)
			{
				cboHomeTabId.Items.FindByValue(objPortal.HomeTabId.ToString()).Selected = true;
			}
			cboLoginTabId.DataSource = listTabs;
			cboLoginTabId.DataBind();
			if (cboLoginTabId.Items.FindByValue(objPortal.LoginTabId.ToString()) != null)
			{
				cboLoginTabId.Items.FindByValue(objPortal.LoginTabId.ToString()).Selected = true;
			}
			cboRegisterTabId.DataSource = listTabs;
			cboRegisterTabId.DataBind();
			if (cboRegisterTabId.Items.FindByValue(objPortal.RegisterTabId.ToString()) != null)
			{
				cboRegisterTabId.Items.FindByValue(objPortal.RegisterTabId.ToString()).Selected = true;
			}
			cboSearchTabId.DataSource = listTabs;
			cboSearchTabId.DataBind();
			if (cboSearchTabId.Items.FindByValue(objPortal.SearchTabId.ToString()) != null)
			{
				cboSearchTabId.Items.FindByValue(objPortal.SearchTabId.ToString()).Selected = true;
			}

			listTabs = TabController.GetPortalTabs(intPortalId, Null.NullInteger, false, true);
			cboUserTabId.DataSource = listTabs;
			cboUserTabId.DataBind();
			if (cboUserTabId.Items.FindByValue(objPortal.UserTabId.ToString()) != null)
			{
				cboUserTabId.Items.FindByValue(objPortal.UserTabId.ToString()).Selected = true;
			}
			Common.Lists.ListEntryInfoCollection colList = ctlList.GetListEntryInfoCollection("Currency");
			cboCurrency.DataSource = colList;
			cboCurrency.DataBind();
			if (Null.IsNull(objPortal.Currency) || cboCurrency.Items.FindByValue(objPortal.Currency) == null)
			{
				cboCurrency.Items.FindByValue("USD").Selected = true;
			}
			else
			{
				cboCurrency.Items.FindByValue(objPortal.Currency).Selected = true;
			}
			DotNetNuke.Security.Roles.RoleController objRoleController = new DotNetNuke.Security.Roles.RoleController();
			ArrayList Arr = objRoleController.GetUserRolesByRoleName(intPortalId, objPortal.AdministratorRoleName);
			int i;
			for (i = 0; i <= Arr.Count - 1; i++)
			{
				UserRoleInfo objUser = (UserRoleInfo)Arr[i];
				cboAdministratorId.Items.Add(new ListItem(objUser.FullName, objUser.UserID.ToString()));
			}
			if (cboAdministratorId.Items.FindByValue(objPortal.AdministratorId.ToString()) != null)
			{
				cboAdministratorId.Items.FindByValue(objPortal.AdministratorId.ToString()).Selected = true;
			}
			if (!Null.IsNull(objPortal.ExpiryDate))
			{
				txtExpiryDate.Text = objPortal.ExpiryDate.ToShortDateString();
			}
			txtHostFee.Text = objPortal.HostFee.ToString();
			txtHostSpace.Text = objPortal.HostSpace.ToString();
			txtPageQuota.Text = objPortal.PageQuota.ToString();
			txtUserQuota.Text = objPortal.UserQuota.ToString();
			if (objPortal.SiteLogHistory != Null.NullInteger)
			{
				txtSiteLogHistory.Text = objPortal.SiteLogHistory.ToString();
			}
			if (!String.IsNullOrEmpty(objPortal.PaymentProcessor))
			{
				if (cboProcessor.Items.FindByText(objPortal.PaymentProcessor) != null)
				{
					cboProcessor.Items.FindByText(objPortal.PaymentProcessor).Selected = true;
				}
				else
				{
					if (cboProcessor.Items.FindByText("PayPal") != null)
					{
						cboProcessor.Items.FindByText("PayPal").Selected = true;
					}
				}
			}
			else
			{
				cboProcessor.Items.FindByValue("").Selected = true;
			}
			txtUserId.Text = objPortal.ProcessorUserId;
			txtPassword.Attributes.Add("value", objPortal.ProcessorPassword);
			lblHomeDirectory.Text = objPortal.HomeDirectory;

			//'PortalSettings for portal being edited
			PortalSettings _PortalSettings = new PortalSettings(objPortal);

			// usability settings
			chkInlineEditor.Checked = _PortalSettings.InlineEditorEnabled;
			chkHideSystemFolders.Checked = _PortalSettings.HideFoldersEnabled;

			if (_PortalSettings.DefaultControlPanelMode == Entities.Portals.PortalSettings.Mode.Edit)
			{
				optControlPanelMode.Items.FindByValue("EDIT").Selected = true;
			}
			else
			{
				optControlPanelMode.Items.FindByValue("VIEW").Selected = true;
			}

			if (_PortalSettings.DefaultControlPanelVisibility)
			{
				optControlPanelVisibility.Items.FindByValue("MAX").Selected = true;
			}
			else
			{
				optControlPanelVisibility.Items.FindByValue("MIN").Selected = true;
			}

			if (_PortalSettings.ControlPanelSecurity == PortalSettings.ControlPanelPermission.ModuleEditor)
			{
				optControlPanelSecurity.Items.FindByValue("MODULE").Selected = true;
			}
			else
			{
				optControlPanelSecurity.Items.FindByValue("TAB").Selected = true;
			}

			chkSSLEnabled.Checked = _PortalSettings.SSLEnabled;
			chkSSLEnforced.Checked = _PortalSettings.SSLEnforced;
			txtSSLURL.Text = _PortalSettings.SSLURL;
			txtSTDURL.Text = _PortalSettings.STDURL;
			//Populate the timezone combobox (look up timezone translations based on currently set culture)
			Services.Localization.Localization.LoadTimeZoneDropDownList(cboTimeZone, (Page as PageBase).PageCulture.Name, Convert.ToString(objPortal.TimeZoneOffset));

			ctlPortalSkin.SkinRoot = SkinController.RootSkin;
			ctlPortalSkin.SkinSrc = _PortalSettings.DefaultPortalSkin;
			ctlPortalContainer.SkinRoot = SkinController.RootContainer;
			ctlPortalContainer.SkinSrc = _PortalSettings.DefaultPortalContainer;
			ctlAdminSkin.SkinRoot = SkinController.RootSkin;
			ctlAdminSkin.SkinSrc = _PortalSettings.DefaultAdminSkin;
			ctlAdminContainer.SkinRoot = SkinController.RootContainer;
			ctlAdminContainer.SkinSrc = _PortalSettings.DefaultAdminContainer;

			chkSkinWidgestEnabled.Checked = _PortalSettings.EnableSkinWidgets;

			string portalAliasMapping = _PortalSettings.PortalAliasMappingMode.ToString().ToUpper();
			if (portalAliasModeButtonList.Items.FindByValue(portalAliasMapping) != null)
			{
				portalAliasModeButtonList.Items.FindByValue(portalAliasMapping).Selected = true;
			}
			else
			{
				portalAliasModeButtonList.Items[0].Selected = true;
			}

			if (portalAliasMapping.ToUpperInvariant() == "NONE")
			{
				defaultAliasRow.Visible = false;
			}
			else
			{
				defaultAliasRow.Visible = true;
				defaultAliasDropDown.DataSource = arrPortalAliases;
				defaultAliasDropDown.DataBind();

				string defaultAlias = _PortalSettings.DefaultPortalAlias;
				if (defaultAliasDropDown.Items.FindByValue(defaultAlias) != null)
				{
					defaultAliasDropDown.Items.FindByValue(defaultAlias).Selected = true;
				}
			}

			//Auto Add Portal Alias
			if (new DotNetNuke.Entities.Portals.PortalController().GetPortals().Count > 1)
			{
				chkAutoAddPortalAlias.Enabled = false;
				chkAutoAddPortalAlias.Checked = false;
			}
			else
			{
				chkAutoAddPortalAlias.Checked = DotNetNuke.Entities.Controllers.HostController.Instance.GetBoolean("AutoAddPortalAlias");
			}


			// use sandbox?
			bool bolPayPalSandbox = bool.Parse(PortalController.GetPortalSetting("paypalsandbox", intPortalId, "False"));
			chkPayPalSandboxEnabled.Checked = bolPayPalSandbox;

			// return url after payment or on cancel
			string strPayPalReturnURL = PortalController.GetPortalSetting("paypalsubscriptionreturn", intPortalId, Null.NullString);
			txtPayPalReturnURL.Text = strPayPalReturnURL;
			string strPayPalCancelURL = PortalController.GetPortalSetting("paypalsubscriptioncancelreturn", intPortalId, Null.NullString);
			txtPayPalCancelURL.Text = strPayPalCancelURL;

			LoadStyleSheet();


		}

		private void LoadStyleSheet()
		{
			string strUploadDirectory = "";
			PortalController objPortalController = new PortalController();
			PortalInfo objPortal = objPortalController.GetPortal(intPortalId);
			if (objPortal != null)
			{
				strUploadDirectory = objPortal.HomeDirectoryMapPath;
			}
			if (System.IO.File.Exists(strUploadDirectory + "portal.css"))
			{
				StreamReader objStreamReader;
				objStreamReader = File.OpenText(strUploadDirectory + "portal.css");
				txtStyleSheet.Text = objStreamReader.ReadToEnd();
				objStreamReader.Close();
			}
		}

		protected override void OnInit(System.EventArgs e)
		{
			base.OnInit(e);
			Framework.AJAX.RegisterPostBackControl(cmdSearchEngine);
			Framework.AJAX.RegisterPostBackControl(cmdSiteMap);
			ctlDesktopModules.LocalResourceFile = LocalResourceFile;

			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdDelete.Click += new EventHandler(cmdDelete_Click);
			cmdProcessor.Click += new EventHandler(cmdProcessor_Click);
			cmdRestore.Click += new EventHandler(cmdRestore_Click);
			cmdSave.Click += new EventHandler(cmdSave_Click);
			cmdSearchEngine.Click += new EventHandler(cmdSearchEngine_Click);
			cmdSiteMap.Click += new EventHandler(cmdSiteMap_Click);
			cmdUpdate.Click += new EventHandler(cmdUpdate_Click);
			cmdVerification.Click += new EventHandler(cmdVerification_Click);
			ctlDesktopModules.AddAllButtonClick += new EventHandler(ctlDesktopModules_AddAllButtonClick);
			ctlDesktopModules.AddButtonClick += new DotNetNuke.UI.WebControls.DualListBoxEventHandler(ctlDesktopModules_AddButtonClick);
			ctlDesktopModules.RemoveAllButtonClick += new EventHandler(ctlDesktopModules_RemoveAllButtonClick);
			ctlDesktopModules.RemoveButtonClick += new DotNetNuke.UI.WebControls.DualListBoxEventHandler(ctlDesktopModules_RemoveButtonClick);
		}
		public string FormatCurrency()
		{
			string retValue = "";
			try
			{
				retValue = Host.HostCurrency + " / " + Services.Localization.Localization.GetString("Month");
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
			return retValue;
		}
		public string FormatFee(object objHostFee)
		{
			string retValue = "";
			try
			{
				if (objHostFee != DBNull.Value)
				{
					retValue = ((float)objHostFee).ToString("#,##0.00");
				}
				else
				{
					retValue = "0";
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
			return retValue;
		}
		public bool IsSubscribed(int PortalModuleDefinitionId)
		{
			return Null.IsNull(PortalModuleDefinitionId) == false;
		}
		public bool IsSuperUser()
		{
			return this.UserInfo.IsSuperUser;
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			try
			{
				if ((Request.QueryString["pid"] != null) && (PortalSettings.ActiveTab.ParentId == PortalSettings.SuperTabId || UserInfo.IsSuperUser))
				{
					intPortalId = Int32.Parse(Request.QueryString["pid"]);
					ctlLogo.ShowUpLoad = false;
					ctlBackground.ShowUpLoad = false;
					cmdCancel.Visible = true;
				}
				else
				{
					intPortalId = PortalId;
					ctlLogo.ShowUpLoad = true;
					ctlBackground.ShowUpLoad = true;
					cmdCancel.Visible = false;
				}

				cmdExpiryCalendar.NavigateUrl = Common.Utilities.Calendar.InvokePopupCal(txtExpiryDate);
				DotNetNuke.UI.Utilities.ClientAPI.AddButtonConfirm(cmdRestore, Services.Localization.Localization.GetString("RestoreCCSMessage", this.LocalResourceFile));
				BindDesktopModules();
				if (Page.IsPostBack == false)
				{
					DotNetNuke.UI.Utilities.ClientAPI.AddButtonConfirm(cmdDelete, Services.Localization.Localization.GetString("DeleteMessage", this.LocalResourceFile));

					LoadPortal(SelectedCultureCode);

					if ((Request.UrlReferrer != null))
					{
						if (Request.UrlReferrer.AbsoluteUri == Request.Url.AbsoluteUri)
						{
							ViewState["UrlReferrer"] = "";
						}
						else
						{
							ViewState["UrlReferrer"] = Convert.ToString(Request.UrlReferrer);
						}
					}
					else
					{
						ViewState["UrlReferrer"] = "";

					}
				}

				if (UserInfo.IsSuperUser)
				{
					dshHost.Visible = true;
					tblHost.Visible = true;
					dshSSL.Visible = true;
					tblSSL.Visible = true;
					dshAliases.Visible = true;
					tblAliases.Visible = true;
					cmdDelete.Visible = (intPortalId != PortalId);
				}
				else
				{
					dshHost.Visible = false;
					tblHost.Visible = false;
					dshSSL.Visible = false;
					tblSSL.Visible = false;
					dshAliases.Visible = false;
					tblAliases.Visible = false;
					cmdDelete.Visible = false;
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}

		private void cmdCancel_Click(object sender, System.EventArgs e)
		{
			try
			{
				Response.Redirect(Common.Globals.NavigateURL(), true);
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdDelete_Click(object sender, EventArgs e)
		{
			try
			{
				PortalController objPortalController = new PortalController();
				PortalInfo objPortalInfo = objPortalController.GetPortal(intPortalId);
				if (objPortalInfo != null)
				{
					string strMessage = PortalController.DeletePortal(objPortalInfo, Common.Globals.GetAbsoluteServerPath(Request));
					if (string.IsNullOrEmpty(strMessage))
					{
						Services.Log.EventLog.EventLogController objEventLog = new Services.Log.EventLog.EventLogController();
						objEventLog.AddLog("PortalName", objPortalInfo.PortalName, PortalSettings, UserId, Services.Log.EventLog.EventLogController.EventLogType.PORTAL_DELETED);
						if (intPortalId == PortalId)
						{
							if (!string.IsNullOrEmpty(Host.HostURL))
							{
								Response.Redirect(Common.Globals.AddHTTP(Host.HostURL));
							}
							else
							{
								Response.End();
							}
						}
						else
						{
							Response.Redirect(Convert.ToString(ViewState["UrlReferrer"]), true);
						}
					}
					else
					{
						UI.Skins.Skin.AddModuleMessage(this, strMessage, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
					}
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdProcessor_Click(object sender, System.EventArgs e)
		{
			try
			{
				Response.Redirect(Common.Globals.AddHTTP(cboProcessor.SelectedItem.Value), true);
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdRestore_Click(object sender, System.EventArgs e)
		{
			try
			{
				string strServerPath = Request.MapPath(Common.Globals.ApplicationPath);
				PortalController objPortalController = new PortalController();
				PortalInfo objPortal = objPortalController.GetPortal(intPortalId);
				if (objPortal != null)
				{
					if (System.IO.File.Exists(objPortal.HomeDirectoryMapPath + "portal.css"))
					{
						System.IO.File.Delete(objPortal.HomeDirectoryMapPath + "portal.css");
					}
					if (File.Exists(Common.Globals.HostMapPath + "portal.css"))
					{
						System.IO.File.Copy(Common.Globals.HostMapPath + "portal.css", objPortal.HomeDirectoryMapPath + "portal.css");
					}
				}
				LoadStyleSheet();
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdSave_Click(object sender, System.EventArgs e)
		{
			try
			{
				string strUploadDirectory = "";
				PortalController objPortalController = new PortalController();
				PortalInfo objPortal = objPortalController.GetPortal(intPortalId);
				if (objPortal != null)
				{
					strUploadDirectory = objPortal.HomeDirectoryMapPath;
				}
				if (File.Exists(strUploadDirectory + "portal.css"))
				{
					File.SetAttributes(strUploadDirectory + "portal.css", FileAttributes.Normal);
				}
				StreamWriter objStream;
				objStream = File.CreateText(strUploadDirectory + "portal.css");
				objStream.WriteLine(txtStyleSheet.Text);
				objStream.Close();
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		private void cmdUpdate_Click(object sender, EventArgs e)
		{
			if (Page.IsValid)
			{
				try
				{
					string strLogo;
					string strBackground;
					bool refreshPage = Null.NullBoolean;
					PortalController objPortalController = new PortalController();
					PortalInfo objPortal = objPortalController.GetPortal(intPortalId);
					strLogo = String.Format("FileID={0}", ctlLogo.FileID);
					strBackground = String.Format("FileID={0}", ctlBackground.FileID);
					refreshPage = (strBackground == objPortal.BackgroundFile || strLogo == objPortal.LogoFile);
					double dblHostFee = 0;
					if (!String.IsNullOrEmpty(txtHostFee.Text))
					{
						dblHostFee = double.Parse(txtHostFee.Text);
					}
					double dblHostSpace = 0;
					if (!String.IsNullOrEmpty(txtHostSpace.Text))
					{
						dblHostSpace = double.Parse(txtHostSpace.Text);
					}
					int intPageQuota = 0;
					if (!String.IsNullOrEmpty(txtPageQuota.Text))
					{
						intPageQuota = int.Parse(txtPageQuota.Text);
					}
					double intUserQuota = 0;
					if (!String.IsNullOrEmpty(txtUserQuota.Text))
					{
						intUserQuota = int.Parse(txtUserQuota.Text);
					}
					int intSiteLogHistory = -1;
					if (!String.IsNullOrEmpty(txtSiteLogHistory.Text))
					{
						intSiteLogHistory = int.Parse(txtSiteLogHistory.Text);
					}
					System.DateTime datExpiryDate = Null.NullDate;
					if (!String.IsNullOrEmpty(txtExpiryDate.Text))
					{
						datExpiryDate = Convert.ToDateTime(txtExpiryDate.Text);
					}
					int intSplashTabId = Null.NullInteger;
					if (cboSplashTabId.SelectedItem != null)
					{
						intSplashTabId = int.Parse(cboSplashTabId.SelectedItem.Value);
					}
					int intHomeTabId = Null.NullInteger;
					if (cboHomeTabId.SelectedItem != null)
					{
						intHomeTabId = int.Parse(cboHomeTabId.SelectedItem.Value);
					}
					int intLoginTabId = Null.NullInteger;
					if (cboLoginTabId.SelectedItem != null)
					{
						intLoginTabId = int.Parse(cboLoginTabId.SelectedItem.Value);
					}
					int intRegisterTabId = Null.NullInteger;
					if (cboRegisterTabId.SelectedItem != null)
					{
						intRegisterTabId = int.Parse(cboRegisterTabId.SelectedItem.Value);
					}
					int intUserTabId = Null.NullInteger;
					if (cboUserTabId.SelectedItem != null)
					{
						intUserTabId = int.Parse(cboUserTabId.SelectedItem.Value);
					}
					int intSearchTabId = Null.NullInteger;
					if (cboSearchTabId.SelectedItem != null)
					{
						intSearchTabId = int.Parse(cboSearchTabId.SelectedItem.Value);
					}

					if (txtPassword.Attributes["value"] != null)
					{
						txtPassword.Attributes["value"] = txtPassword.Text;
					}
					if (!UserInfo.IsSuperUser)
					{
						bool HostChanged = false;
						if (dblHostFee != objPortal.HostFee)
							HostChanged = true;
						if (dblHostSpace != objPortal.HostSpace)
							HostChanged = true;
						if (intPageQuota != objPortal.PageQuota)
							HostChanged = true;
						if (intUserQuota != objPortal.UserQuota)
							HostChanged = true;
						if (intSiteLogHistory != objPortal.SiteLogHistory)
							HostChanged = true;
						if (datExpiryDate != objPortal.ExpiryDate)
							HostChanged = true;
						if (HostChanged == true)
						{
							throw new System.Exception();
						}
					}

					objPortalController.UpdatePortalInfo(intPortalId, txtPortalName.Text, strLogo, txtFooterText.Text, datExpiryDate, optUserRegistration.SelectedIndex, optBanners.SelectedIndex, cboCurrency.SelectedItem.Value, Convert.ToInt32(cboAdministratorId.SelectedItem.Value), dblHostFee,
						dblHostSpace, intPageQuota, (int)intUserQuota, String.IsNullOrEmpty(cboProcessor.SelectedValue) ? "" : cboProcessor.SelectedItem.Text, txtUserId.Text, txtPassword.Text, txtDescription.Text, txtKeyWords.Text, strBackground, intSiteLogHistory,
					intSplashTabId, intHomeTabId, intLoginTabId, intRegisterTabId, intUserTabId, intSearchTabId, objPortal.DefaultLanguage, Convert.ToInt32(cboTimeZone.SelectedValue), lblHomeDirectory.Text, SelectedCultureCode);
					
					if (!refreshPage)
					{
						refreshPage = (PortalSettings.DefaultAdminSkin == ctlAdminSkin.SkinSrc) || (PortalSettings.DefaultAdminContainer == ctlAdminContainer.SkinSrc);
					}

					PortalController.UpdatePortalSetting(intPortalId, "EnableSkinWidgets", chkSkinWidgestEnabled.Checked.ToString(), false);
					PortalController.UpdatePortalSetting(intPortalId, "DefaultAdminSkin", ctlAdminSkin.SkinSrc, false);
					PortalController.UpdatePortalSetting(intPortalId, "DefaultPortalSkin", ctlPortalSkin.SkinSrc, false);
					PortalController.UpdatePortalSetting(intPortalId, "DefaultAdminContainer", ctlAdminContainer.SkinSrc, false);
					PortalController.UpdatePortalSetting(intPortalId, "DefaultPortalContainer", ctlPortalContainer.SkinSrc, false);
					PortalController.UpdatePortalSetting(intPortalId, "InlineEditorEnabled", chkInlineEditor.Checked.ToString(), false);
					PortalController.UpdatePortalSetting(intPortalId, "HideFoldersEnabled", chkHideSystemFolders.Checked.ToString(), false);
					PortalController.UpdatePortalSetting(intPortalId, "ControlPanelMode", optControlPanelMode.SelectedItem.Value, false);
					PortalController.UpdatePortalSetting(intPortalId, "ControlPanelVisibility", optControlPanelVisibility.SelectedItem.Value, false);
					PortalController.UpdatePortalSetting(intPortalId, "ControlPanelSecurity", optControlPanelSecurity.SelectedItem.Value, false);
					PortalController.UpdatePortalSetting(intPortalId, "paypalsandbox", chkPayPalSandboxEnabled.Checked.ToString(), false);
					PortalController.UpdatePortalSetting(intPortalId, "paypalsubscriptionreturn", txtPayPalReturnURL.Text, false);
					PortalController.UpdatePortalSetting(intPortalId, "paypalsubscriptioncancelreturn", txtPayPalCancelURL.Text, false);
					PortalController.UpdatePortalSetting(intPortalId, "PortalAliasMapping", portalAliasModeButtonList.SelectedValue, false);
					PortalController.UpdatePortalSetting(intPortalId, "DefaultPortalAlias", defaultAliasDropDown.SelectedValue, false);
					DotNetNuke.Entities.Controllers.HostController.Instance.Update("AutoAddPortalAlias", chkAutoAddPortalAlias.Checked ? "Y" : "N", true);
					if (IsSuperUser())
					{
						PortalController.UpdatePortalSetting(intPortalId, "SSLEnabled", chkSSLEnabled.Checked.ToString(), false);
						PortalController.UpdatePortalSetting(intPortalId, "SSLEnforced", chkSSLEnforced.Checked.ToString(), false);
						PortalController.UpdatePortalSetting(intPortalId, "SSLURL", AddPortalAlias(txtSSLURL.Text, intPortalId), false);
						PortalController.UpdatePortalSetting(intPortalId, "STDURL", AddPortalAlias(txtSTDURL.Text, intPortalId), false);
					}
					if (refreshPage)
						Response.Redirect(Request.RawUrl, true);
					LoadPortal(SelectedCultureCode);
				}
				catch (Exception exc)
				{
					Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
				}
				finally
				{
					DataCache.ClearPortalCache(intPortalId, false);
				}
			}
		}
		private string AddPortalAlias(string PortalAlias, int PortalID)
		{
			if (!String.IsNullOrEmpty(PortalAlias))
			{
				if (PortalAlias.IndexOf("://") != -1)
				{
					PortalAlias = PortalAlias.Remove(0, PortalAlias.IndexOf("://") + 3);
				}
				PortalAliasController objPortalAliasController = new PortalAliasController();
				PortalAliasInfo objPortalAlias = objPortalAliasController.GetPortalAlias(PortalAlias, PortalID);
				if (objPortalAlias == null)
				{
					objPortalAlias = new PortalAliasInfo();
					objPortalAlias.PortalID = PortalID;
					objPortalAlias.HTTPAlias = PortalAlias;
					objPortalAliasController.AddPortalAlias(objPortalAlias);
				}
			}
			return PortalAlias;
		}
		protected void cmdSearchEngine_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (cboSearchEngine.SelectedItem != null)
				{
					string strURL = "";
					switch (cboSearchEngine.SelectedItem.Text)
					{
						case "Google":
							strURL += "http://www.google.com/addurl?q=" + Common.Globals.HTTPPOSTEncode(Common.Globals.AddHTTP(Common.Globals.GetDomainName(Request)));
							strURL += "&dq=";
							if (!String.IsNullOrEmpty(txtPortalName.Text))
							{
								strURL += Common.Globals.HTTPPOSTEncode(txtPortalName.Text);
							}
							if (!String.IsNullOrEmpty(txtDescription.Text))
							{
								strURL += Common.Globals.HTTPPOSTEncode(txtDescription.Text);
							}
							if (!String.IsNullOrEmpty(txtKeyWords.Text))
							{
								strURL += Common.Globals.HTTPPOSTEncode(txtKeyWords.Text);
							}
							strURL += "&submit=Add+URL";
							break;
						case "Yahoo":
							strURL = "http://siteexplorer.search.yahoo.com/submit";
							break;
						case "Microsoft":
							strURL = "http://search.msn.com.sg/docs/submit.aspx";
							break;
					}
					UrlUtils.OpenNewWindow(this.Page, this.GetType(), strURL);
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void cmdSiteMap_Click(object sender, System.EventArgs e)
		{
			UrlUtils.OpenNewWindow(this.Page, this.GetType(), "http://www.google.com/webmasters/sitemaps/");
		}
		protected void cmdVerification_Click(object sender, System.EventArgs e)
		{
			if (!String.IsNullOrEmpty(txtVerification.Text) && txtVerification.Text.EndsWith(".html"))
			{
				if (!File.Exists(Common.Globals.ApplicationMapPath + "\\" + txtVerification.Text))
				{
					StreamWriter objStream;
					objStream = File.CreateText(Common.Globals.ApplicationMapPath + "\\" + txtVerification.Text);
					objStream.WriteLine("google-site-verification: " + txtVerification.Text);
					objStream.Close();
				}
			}
		}
		protected void ctlDesktopModules_AddAllButtonClick(object sender, System.EventArgs e)
		{
			foreach (DesktopModuleInfo desktopModule in DesktopModuleController.GetDesktopModules(Null.NullInteger).Values)
			{
				DesktopModuleController.AddDesktopModuleToPortal(intPortalId, desktopModule.DesktopModuleID, true, false);
			}
			DataCache.ClearPortalCache(intPortalId, false);
			BindDesktopModules();
		}
		protected void ctlDesktopModules_AddButtonClick(object sender, UI.WebControls.DualListBoxEventArgs e)
		{
			if (e.Items != null)
			{
				foreach (string desktopModule in e.Items)
				{
					DesktopModuleController.AddDesktopModuleToPortal(intPortalId, int.Parse(desktopModule), true, false);
				}
			}
			DataCache.ClearPortalCache(intPortalId, false);
			BindDesktopModules();
		}
		protected void ctlDesktopModules_RemoveAllButtonClick(object sender, System.EventArgs e)
		{
			foreach (DesktopModuleInfo desktopModule in DesktopModuleController.GetDesktopModules(Null.NullInteger).Values)
			{
				DesktopModuleController.RemoveDesktopModuleFromPortal(intPortalId, desktopModule.DesktopModuleID, false);
			}
			DataCache.ClearPortalCache(intPortalId, false);
			BindDesktopModules();
		}
		protected void ctlDesktopModules_RemoveButtonClick(object sender, UI.WebControls.DualListBoxEventArgs e)
		{
			if (e.Items != null)
			{
				foreach (string desktopModule in e.Items)
				{
					DesktopModuleController.RemoveDesktopModuleFromPortal(intPortalId, int.Parse(desktopModule), false);
				}
			}
			DataCache.ClearPortalCache(intPortalId, false);
			BindDesktopModules();
		}
	}
}
